package com.example.maoyanmovie.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import androidx.appcompat.app.AppCompatActivity;
import com.bumptech.glide.Glide;
import com.example.maoyanmovie.R;
import com.example.maoyanmovie.databinding.ActivityMovieDetailBinding;
import com.example.maoyanmovie.model.Movie;
import com.example.maoyanmovie.utils.Constants;

public class MovieDetailActivity extends AppCompatActivity {

    private ActivityMovieDetailBinding binding;
    private Movie movie;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityMovieDetailBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        // 获取传递的电影对象
        movie = (Movie) getIntent().getSerializableExtra(Constants.INTENT_KEY_MOVIE);

        initView();
        loadMovieDetail();
    }

    private void initView() {
        binding.ivBack.setOnClickListener(v -> finish());

        binding.btnBuy.setOnClickListener(v -> {
            Intent intent = new Intent(this, SeatSelectionActivity.class);
            if (movie != null) {
                intent.putExtra(Constants.INTENT_KEY_MOVIE, movie);
            }
            startActivity(intent);
        });
    }

    private void loadMovieDetail() {
        if (movie == null) {
            binding.tvName.setText("电影信息加载失败");
            return;
        }

        // 显示电影名称
        binding.tvName.setText(movie.getName());

        // 显示评分
        if (movie.getScore() > 0) {
            binding.tvScore.setVisibility(View.VISIBLE);
            binding.tvScore.setText(String.valueOf(movie.getScore()));
        } else {
            binding.tvScore.setVisibility(View.GONE);
        }

        // 显示主演
        if (!TextUtils.isEmpty(movie.getStar())) {
            binding.tvStar.setText("主演：" + movie.getStar());
        } else {
            binding.tvStar.setText("主演：暂无信息");
        }

        // 显示上映时间
        if (!TextUtils.isEmpty(movie.getReleaseTime())) {
            binding.tvReleaseTime.setText("上映时间：" + movie.getReleaseTime());
        } else {
            binding.tvReleaseTime.setText("上映时间：暂无信息");
        }

        // 显示剧情简介（使用showInfo作为简介）
        if (!TextUtils.isEmpty(movie.getShowInfo())) {
            binding.tvDescription.setText(movie.getShowInfo());
        } else {
            binding.tvDescription.setText("暂无剧情简介");
        }

        // 加载海报
        if (!TextUtils.isEmpty(movie.getImageUrl())) {
            Glide.with(this)
                    .load(movie.getImageUrl())
                    .placeholder(R.mipmap.ic_launcher)
                    .error(R.mipmap.ic_launcher)
                    .into(binding.ivPoster);
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        binding = null;
    }
}
